#!/usr/bin/env python3
"""
安居客租房数据爬虫Web应用启动脚本
"""

import os
import sys
from app import app


def check_dependencies():
    """检查依赖是否安装"""
    try:
        import flask
        import requests
        import pandas
        import pymysql
        print("✓ 所有依赖包已安装")
        return True
    except ImportError as e:
        print(f"✗ 缺少依赖包: {e}")
        print("请运行: pip install -r requirements.txt")
        return False


def check_directories():
    """检查必要的目录是否存在"""
    directories = ['uploads', 'static', 'templates', 'spider']

    for directory in directories:
        if not os.path.exists(directory):
            print(f"创建目录: {directory}")
            os.makedirs(directory)

    print("✓ 所有目录已就绪")


def main():
    """主函数"""
    print("=" * 50)
    print("   安居客租房数据爬虫Web应用")
    print("=" * 50)

    # 检查依赖
    if not check_dependencies():
        sys.exit(1)

    # 检查目录
    check_directories()

    # 启动应用
    print("✓ 启动Flask应用...")
    print("✓ 访问地址: http://localhost:5000")
    print("✓ 按 Ctrl+C 停止服务")

    app.run(debug=True, host='0.0.0.0', port=5000)


if __name__ == '__main__':
    main()